//
//  YDYRecommendTagViewController.m
//  DYSisterAPP
//
//  Created by 杨德勇 on 2016/8/8.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYRecommendTagViewController.h"
#import "YDYHTTPSessionManager.h"
#import "YDYRecommendCell.h"
#import <MJExtension.h>
#import "YDYRecommendTag.h"
#import <SVProgressHUD.h>
@interface YDYRecommendTagViewController ()
/** 网络控制 */
@property (nonatomic, strong)  YDYHTTPSessionManager*manager;
/** 数据源数组 */
@property (nonatomic, strong) NSArray<YDYRecommendTag *> *recommendTags;
@end

@implementation YDYRecommendTagViewController

#pragma mark -
#pragma mark -cell的重用标识符
static NSString * const commmendCellId = @"recommend";

#pragma mark -
#pragma mark -网络请求的控制器(manager)
-(YDYHTTPSessionManager *)manager{
    if (!_manager) {
        _manager = [[YDYHTTPSessionManager alloc]init];
    }
    return  _manager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"推荐标签";
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YDYRecommendCell class]) bundle:nil] forCellReuseIdentifier:commmendCellId];
    self.tableView.rowHeight = 85;
    self.tableView.backgroundColor = [UIColor darkGrayColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //加载数据
    [self loadNewRecommendTags];
}

-(void)loadNewRecommendTags{
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"a"] = @"tag_recommend";
    params[@"action"] = @"sub";
    params[@"c"] = @"topic";
    //为了让控制器早点消失,把控制器改成弱指针
    __weak typeof(self) weakself = self ;
    [self.manager GET:YDYcommentURL parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        [SVProgressHUD show];
        //请求进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //请求成功
        weakself.recommendTags = [YDYRecommendTag mj_objectArrayWithKeyValuesArray:responseObject];
        
        [weakself.tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //请求失败
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:@"网络繁忙"];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
    }];
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //消除动画
    [SVProgressHUD dismiss];
    //清除网络请求，控制器要挂了，所有摸掉session更好，这样不会调用error方法
    //*注意 别在控制器有调转界面的时候这样做，再push网络就没了，只能在ERROR里面筛选
    [self.manager invalidateSessionCancelingTasks:YES];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.recommendTags.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YDYRecommendCell *cell = [self.tableView dequeueReusableCellWithIdentifier:commmendCellId];
    cell.recommend = self.recommendTags[indexPath.row];
    return cell;
    
}
@end
